package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.base.BaseController;
import com.atguigu.entity.Admin;
import com.atguigu.entity.HouseBroker;
import com.atguigu.service.AdminService;
import com.atguigu.service.HouseBrokerService;
import lombok.extern.slf4j.Slf4j;
import org.checkerframework.checker.nullness.qual.NonNull;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import java.util.List;

/**
 * @PACKAGE_NAME: com.atguigu.controller
 * @CLASSNAME: HouseBrokerController
 * @AUTHOR: zhangsan
 * @DATE: 2024/4/23 22:19
 * @SINCE 17.0.7
 * @DESCRIPTION: HouseBrokerController
 */
@Slf4j
@Controller
@RequestMapping("/houseBroker")
public class HouseBrokerController extends BaseController {
    @Reference
    private HouseBrokerService houseBrokerService;

    @Reference
    private AdminService adminService;
    private static final String PAGE_CREATE = "houseBroker/create";
    private static final String PAGE_EDIT = "houseBroker/edit";
    private static final String LIST_ACTION = "redirect:/house/";

    private static final String PAGE_SUCCESS = "common/successPage";

    @RequestMapping("/create/{houseId}")
    public String toCreatePage(@NonNull Model model,@PathVariable("houseId") Long houseId) {
        List<Admin> adminList = adminService.findAll();
        log.debug("houseId = {}", houseId);
        model.addAttribute("houseId", houseId);
        model.addAttribute("adminList", adminList);
        return PAGE_CREATE;
    }

    /**
     * <input type="hidden" name="houseId" th:value="${houseId}"/>
     * <select name="brokerId" id="brokerId" class="form-control">
     *     <option value="">-请选择-</option>
     *     <option th:each="item,it : ${adminList}" th:text="${item.name}" th:value="${item.id}">-请选择-</option>
     * </select>
     * 表单实际上 只提交了一个 houseId,一个brokerId,通过brokerId作为acl_admin管理员表的id,去查询
     * 管理员,之后填充houseBroker,毕竟经纪人就是从房屋管理员
     * @NonNull HouseBroker houseBroker,
     * @return
     */
    @RequestMapping("/save")
    public String insertHouseBroker(HouseBroker houseBroker) {
        log.debug("houseBroker = {}", houseBroker);
        Admin admin = adminService.getById(houseBroker.getBrokerId());
        houseBroker.setBrokerName(admin.getName());
        houseBroker.setBrokerHeadUrl(admin.getHeadUrl());
        Integer i = houseBrokerService.insert(houseBroker);
        return PAGE_SUCCESS;
    }

    @RequestMapping("/edit/{houseId}/{id}")
    public String toEditPage(@PathVariable("houseId") Long houseId,
                           @PathVariable("id") Long id,
                           Model model) {
        HouseBroker houseBroker = houseBrokerService.getById(id);
        List<Admin> adminList = adminService.findAll();
        model.addAttribute("houseBroker", houseBroker);
        model.addAttribute("adminList", adminList);
        return PAGE_EDIT;
    }
    /**
     * 改进updateHouseBroker2
     * 传过来两个值,一个是houseBroker对应的hse_house_broker表的主键id, 一个是字段broker_id
     * 因为修改,所以house_id已经有值了,而且不用修改
     * @param houseBroker
     * @return
     */
    @RequestMapping("/update")
    public String updateHouseBroker(HouseBroker houseBroker) {
        Admin admin = adminService.getById(houseBroker.getBrokerId());
        houseBroker.setBrokerName(admin.getName());
        houseBroker.setBrokerHeadUrl(admin.getHeadUrl());
        Integer update = houseBrokerService.update(houseBroker);
        return PAGE_SUCCESS;
    }

    /**
     * 传过来两个值,一个是houseBroker对应的hse_house_broker表的主键id, 一个是字段broker_id
     * @param houseBroker
     * @return
     */
    @RequestMapping("/update2")
    public String updateHouseBroker2(HouseBroker houseBroker) {
        //修改前的HouseBroker
        //旧的broker_id,broker_name,broker_head_url
        //对应admin表的id,name,head_url
        HouseBroker originalHouseBroker = houseBrokerService.getById(houseBroker.getId());
        //要修改的值,从acl_admin表中去拿,hse_house_broker表的broker_id就是acl_admin表的主键id
        Admin admin = adminService.getById(houseBroker.getBrokerId());
        if (houseBroker.getBrokerId().equals(admin.getId())) {
            originalHouseBroker.setBrokerId(houseBroker.getBrokerId());
            originalHouseBroker.setBrokerName(admin.getName());
            originalHouseBroker.setBrokerHeadUrl(admin.getHeadUrl());
            Integer update = houseBrokerService.update(originalHouseBroker);
        }
        return PAGE_SUCCESS;
    }
    @RequestMapping("/delete/{houseId}/{id}")
    public String deleteHouseBroker(@PathVariable("houseId") Long houseId,
                                    @PathVariable("id") Long id) {
        Integer delete = houseBrokerService.delete(id);
        return LIST_ACTION + houseId;
    }
}
